/*
 * Copyright (c) 2017 - present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.rxcv4j;

import com.cv4j.core.binary.Threshold;
import com.cv4j.core.datamodel.ByteProcessor;
import com.cv4j.core.datamodel.CV4JImage;

import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.functions.Function;

/**
 * rx阈值
 *
 * @since 2021-05-17
 */
public class RxThreshold {
    private Single<CV4JImage> single;
    private int type = 0;
    private int method = 0;
    private int thresh = 0;

    /**
     * rx阈值
     *
     * @param image 图像
     */
    private RxThreshold(CV4JImage image) {
        single = Single.just(image);
    }

    /**
     * 图像
     *
     * @param image 图像
     * @return {@link RxThreshold}
     */
    public static RxThreshold image(CV4JImage image) {
        return new RxThreshold(image);
    }

    /**
     * 类型
     *
     * @param type 类型
     * @return {@link RxThreshold}
     */
    public RxThreshold type(int type) {
        this.type = type;
        return this;
    }

    /**
     * 方法
     *
     * @param method 方法
     * @return {@link RxThreshold}
     */
    public RxThreshold method(int method) {
        this.method = method;
        return this;
    }

    /**
     * 打
     *
     * @param thresh 打
     * @return {@link RxThreshold}
     */
    public RxThreshold thresh(int thresh) {
        this.thresh = thresh;
        return this;
    }

    /**
     * 过程
     *
     * @return {@link Single<ByteProcessor>}
     */
    public Single<ByteProcessor> process() {
        return single.map(new Function<CV4JImage, ByteProcessor>() {
            @Override
            public ByteProcessor apply(CV4JImage cv4JImage) throws Throwable {
                Threshold threshold = new Threshold();
                threshold.process((ByteProcessor) cv4JImage.convert2Gray().getProcessor(), type, method, thresh);
                return (ByteProcessor) cv4JImage.convert2Gray().getProcessor();
            }
        });
    }
}
